using UnityEngine; using UnityEngine.UI; using System.Collections; namespace EnhancedScrollerDemos.SnappingDemo { /// /// This class displays a popup panel when the user wins some credits /// public class PlayWin : MonoBehaviour { /// /// Cached transform to speed up processing /// private Transform _transform; /// /// The amount of time left before moving to the next state /// private float _timeLeft; /// /// This is the UI text element to show the score /// public Text scoreText; /// /// The amount of time to zoom in /// public float zoomTime; /// /// The amount of time to hold in place /// public float holdTime; /// /// The amount of time to disappear /// public float unZoomTime; void Awake() { // cache the transform and hide the panel _transform = transform; _transform.localScale = Vector3.zero; } /// /// This function intiates the playing of the panel /// /// public void Play(int score) { // set the score text scoreText.text = string.Format("{0:n0}", score); // hide the panel to start transform.localScale = Vector3.zero; // reset the timer to the zoom time and start the play zoom _timeLeft = zoomTime; StartCoroutine(PlayZoom()); } /// /// This function makes the panel larger over time /// /// IEnumerator PlayZoom() { while (_timeLeft > 0) { // decrement the timer _timeLeft -= Time.deltaTime; // set the scale of the transform between hidden and showing based on the amount of time left _transform.localScale = Vector3.Lerp(Vector3.zero, Vector3.one, (zoomTime - _timeLeft) / zoomTime); yield return null; } // set the transform to full showing (in case our Lerp didn't quite finish at 1) transform.localScale = Vector3.one; // reset the timer to the hold time and play the hold _timeLeft = holdTime; StartCoroutine(PlayHold()); } /// /// This function waits for a set amount of time /// /// IEnumerator PlayHold() { while (_timeLeft > 0) { // decrement the timer _timeLeft -= Time.deltaTime; yield return null; } // reset the timer to the unzoom time and play the unzoom _timeLeft = unZoomTime; StartCoroutine(PlayUnZoom()); } /// /// This function hides the panel over time /// /// IEnumerator PlayUnZoom() { while (_timeLeft > 0) { // decrement the timer _timeLeft -= Time.deltaTime; // set the scale of the transform between showing and hidden based on the amount of time left _transform.localScale = Vector3.Lerp(Vector3.one, Vector3.zero, (unZoomTime - _timeLeft) / unZoomTime); yield return null; } // set the transform to full hidden (in case our Lerp didn't quite finish at 0) transform.localScale = Vector3.zero; } } }